﻿var $globalVars = {
    isAuthenticated: false,
    userName: null
};
function _tmpl(template, data, target) {
    var tmplItem = template.tmpl(data).appendTo(target).tmplItem();
    var loadHandler = template.data('handler-load');
    if (typeof (loadHandler) == 'function') { loadHandler(tmplItem); }
    return tmplItem;
}
function $tmpl(templateSelector, data, targetSelector) {
    // validate input parameter(s)
    var template = $(templateSelector);
    if (template == null || template.length == 0) { return; }
    var target = $(targetSelector);
    if (target == null || target.length == 0) { return; }
    // create array for tmplItems
    var tmplItems = new Array();
    // apply templates
    if ($.isArray(data)) {
        for (var i = 0; i < data.length; i++) { tmplItems.push(_tmpl(template, data[i], target)); }
    }
    else { tmplItems.push(_tmpl(template, data, target)); }
    // return all tmplItems
    return tmplItems;
}
function $extendTmplItem(tmplItem) {
    // clean up
    if (tmplItem.eventObject != null) {
        tmplItem.eventObject.unbind().remove();
    }
    // add event engine to tmplItem
    tmplItem.eventObject = $('<div>');
    tmplItem.on = function (eventName, handler) {
        tmplItem.eventObject.on(eventName, handler);
    };
    tmplItem.trigger = function (eventName, parameters) {
        tmplItem.eventObject.trigger(eventName, parameters);
    };
}
function $createContainer() {
    return $('<div>')
        .attr('id', Math.ceil(Math.random() * 10000))
        .appendTo(document.body);
}
function $readLines(text) {
    var arr1 = text.split('\n');
    var arr2 = new Array();
    for (var i in arr1) {
        arr2.push(arr1[i].split('\r'));
    }
    var result = new Array();
    for (var i in arr2) {
        var val = $.trim(arr2[i]);
        if (val != null && val.length > 0) { result.push(val); }
    }
    return result;
}
function $writeLines(lines) {
    var result = '';
    for (var i in lines) {
        var val = $.trim(lines[i]);
        if (val != null && val.length > 0) { result += val + '\r'; }
    }
    return $.trim(result);
}
function $confirm(message, onSubmit) {
    var container = $createContainer();
    var tmplItems = $tmpl('#confirmTemplate', message, '#' + container.attr('id'));
    tmplItems[0].trigger('showDialog', function () { onSubmit(); container.remove(); });
}
function $message(message, title) {
    var container = $createContainer();
    if (title) { container.attr('title', title); }
    var tmplItems = $tmpl('#messageTemplate', message, '#' + container.attr('id'));
    tmplItems[0].trigger('showDialog');
}
function $format() {
    var args = arguments;
    return args[0].replace(/{(\d+)}/g, function (match, number) {
        number = parseInt(number, 10);
        return typeof (args[number + 1]) != 'undefined' ? args[number + 1] : '{' + number + '}';
    });
};
function $getCookie(c_name) {
    var i, x, y, ARRcookies = document.cookie.split(';');
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf('='));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf('=') + 1);
        x = x.replace(/^\s+|\s+$/g, '');
        if (x == c_name) { return unescape(y); }
    }
}
function $setCookie(c_name, value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? '' : '; expires=' + exdate.toUTCString());
    document.cookie = c_name + '=' + c_value;
}
function $registerAjaxWaitingElement(selector) {
    var obj = $(selector);
    var doc = $(document);
    doc.ajaxStart(function () { obj.show(); });
    doc.ajaxStop(function () { obj.hide(); });
}
(function ($) {
    $.fn.disableDuringAjax = function() {
        this.each(function() {
            var btn = $(this);
            btn.unbind('ajaxStart').ajaxStart(function() { btn.button('disable'); });
            btn.unbind('ajaxStop').ajaxStop(function() { btn.button('enable'); });
        });
    };
})(jQuery);